<template>
  <el-dialog :visible="showDialog" title="新增员工" width="30%" @close="closeDialog">
    <el-form label-width="120px" :model="formData" ref="formDataRef" :rules="formDataRules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width: 90%" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width: 90%" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期" style="width: 60%">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <!-- 下拉框 -->
        <el-select style="width: 60%" v-model="formData.formOfEmployment">
          <el-option v-for="item in EmployeesEnum.hireType" :key="item.id" :label="item.value" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input style="width: 90%" v-model="formData.workNumber"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- 焦点事件，发送请求获取部门数据 -->
        <el-input style="width: 90%" v-model="formData.departmentName" @focus="getDepartments">
        </el-input>
        <!-- 放置树形组件 -->
        <el-tree v-loading="loading" v-show="showTree" :data="departments" :props="deptsTreeProps"
          :default-expand-all="true" @node-click="selectNode"></el-tree>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" type="date" placeholder="选择转正日期" style="width: 60%">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <!-- dialog 底部 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="btnOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// 导入API
import { addEmployee } from "@/api/employees.js";
import { getDepartments } from "@/api/departments";
import EmployeesEnum from "@/api/constant/employees";
// 引入utils
import { listToTree } from "@/utils";

export default {
  components: {  },
  props: {
    // 控制dialog显示
    showDialog: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "添加员工",
    },
  },
  data() {
    return {
      // 表单数据
      formData: {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      // 表单检验规则
      formDataRules: {
        username: [
          { required: true, message: "员工姓名不能为空", trigger: "blur" },
          { min: 2, max: 10, message: "员工姓名长度在2-10", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
            message: "手机号码格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          // 触发方式是change，避免使用tree选择时校验
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
      // 部门数据
      departments: [],
      // 部门树形控件的配置
      deptsTreeProps: {
        label: "name",
        children: "children",
      },
      // 树形组件的显示与否
      showTree: false,
      // 加载动画
      loading: false,
      // 聘用形式
      EmployeesEnum: EmployeesEnum,
    };
  },
  created() {},
  mounted() {},
  methods: {
    // 关闭弹层
    closeDialog() {
      this.$emit("update:showDialog", false);
      // 重置表单和校验
      this.$refs.formDataRef.resetFields();
    },
    // 请求部门数据
    async getDepartments() {
      this.loading = true;
      const { depts } = await getDepartments();
      // depts是一个数组，转化成树形结构才可以被 el-tree显示
      this.departments = listToTree(depts, "");
      // 显示树形控件
      this.showTree = true;
      this.loading = false;
    },
    // 选择tree节点
    selectNode(node) {
      this.formData.departmentName = node.name;
      // 隐藏tree
      this.showTree = false;
    },
    // 点击确定
    btnOk() {
      this.$refs.formDataRef.validate(async (valid) => {
        if (valid) {
          // 发送请求，添加员工
          await addEmployee(this.formData);
          // 通知父组件刷新,this.$parent 是父组件的实例对象,通过此来调用父组件的方法
          // 先判断是否存在，再调用
          this.$parent.getEmployeeList && this.$parent.getEmployeeList();
          // 关闭弹层
          this.closeDialog();
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
</style>